// pages/address/address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mesList: [],
    id: 0,
    key: 0,
    go: 1
  },
  // 获取地址内容
  request: function () {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/addr/findAll',
      data: {
        userId: wx.getStorageSync("userId")
      },
      method: 'post',
      success: function (res) {
        wx.hideLoading();
        console.log(res);
        that.setData({
          mesList: res.data.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  // 设置默认地址
  content: function (e) {


    var id = e.currentTarget.dataset.index;
    var index = e.currentTarget.dataset.key;
    console.log(index);
    var that = this;
   

    console.log(that.data.id)
    wx.showModal({
      title: '提示',
      content: '确认将该地址设为收货地址 ',
      success: function (res) {
        if (res.confirm) {
          wx.setStorageSync("addressId", id);
          wx.setStorageSync("addressIndex", index);
          that.setData({
            id: index
          })
          console.log(that.data.go);
          if(that.data.go==1){  
          wx.navigateBack({
            delta: 1
          })
        }else{
            wx.navigateBack({
              delta: parseInt(that.data.go) 
            })
        }
        }


      }
    })

  },
  // 编辑地址
  edit: function (e) {
    var index = e.currentTarget.dataset.addid;
    // console.log(index)
    wx.redirectTo({
      url: '../addressAdmin/addressAdmin?addrId=' + index,
    })
  },
  // 删除地址
  delete: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.showModal({
      title: '确认删除该地址',
      content: '',
      cancelText: "取消",
      confirmText: "确认",
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.xiaozheng8.com/addr/delete',
            data: {
              addrId: id,

            },
            method: 'post',

            success: function (res) {
              console.log(res);

              console.log("删除地址");
              that.request();
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      }
    })



  },
  // 添加地址
  address: function () {
    wx.navigateTo({
      url: '../addressAdmin/addressAdmin?go='+2,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      go: options.go
    }

    );
    if (wx.getStorageSync("addressIndex") == null || wx.getStorageSync("addressIndex") == undefined || wx.getStorageSync("addressIndex") == "") {
      that.setData({
        id: 0
      })
    } else {
      that.setData({
        id: wx.getStorageSync("addressIndex")
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.request();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})